package com.iqik.consumer;

import com.iqik.service.DelayService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Configuration // dubbo配置类
@PropertySource("classpath:/dubbo-consumer.properties")
@ComponentScan(basePackages = "com.iqik.consumer")
@EnableDubbo
public class ConsumerMain {

    private static final Logger LOGGER = LoggerFactory.getLogger(ConsumerMain.class);

    @Component
    private class ConsumerComponent {

        @Reference
        private DelayService service;

        public String methodA() {
            return service.methodA();
        }

        public String methodB() {
            return service.methodB();
        }

        public String methodC() {
            return service.methodC();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerMain.class);
        context.start();
        // 获取消费者组件
        ConsumerComponent service = context.getBean(ConsumerComponent.class);
        Thread ma = new Thread(new Runnable() {
            @Override
            public void run() {
                LOGGER.info("method-A result: {}", service.methodA());
            }
        });
        Thread mb = new Thread(new Runnable() {
            @Override
            public void run() {
                LOGGER.info("method-B result: {}", service.methodB());
            }
        });
        Thread mc = new Thread(new Runnable() {
            @Override
            public void run() {
                LOGGER.info("method-C result: {}", service.methodC());
            }
        });
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        while (true) {
            for (int i = 0; i < 2000; i++) {
                executorService.execute(ma);
                executorService.execute(mb);
                executorService.execute(mc);
                Thread.sleep(10);
            }
            Thread.sleep(5000);
        }
    }
}
